from django.contrib.gis.db import models
from django.utils.translation import ugettext_lazy as _

class Country(models.Model):
	__module__ = 'duetopia.register.models'
	name = models.CharField(max_length=256, verbose_name=_('name'))
	objects = models.GeoManager()
	class Meta:
		verbose_name = _('country')
		verbose_name_plural = _('countries')
	def __str__(self):
		return self.name

class Region(models.Model):
	__module__ = 'duetopia.register.models'
	name = models.CharField(max_length=255, verbose_name=_('name'))
	country = models.ForeignKey(Country, null=True, verbose_name=_('country'))
	objects = models.GeoManager()
	class Meta:
		verbose_name = _('region')
		verbose_name_plural = _('regions')
	def __str__(self):
		return self.name

class City(models.Model):
	__module__ = 'duetopia.register.models'
	name = models.CharField(max_length=255, verbose_name=_('name'))
	region = models.ForeignKey(Region, null=True, verbose_name=_('region'))
	objects = models.GeoManager()
	class Meta:
		verbose_name = _('city')
		verbose_name_plural = _('cities')
	def __str__(self):
		return self.name

class LocationManager(models.GeoManager):
	def get_or_create(self, *av, **kw):
		if 'country' in kw:
			if isinstance(kw['country'], basestring):
				country, created = Country.objects.get_or_create(name=kw['country'])
				kw['country'] = country
			else: country = kw['country']
		else: country = None

		if 'region' in kw:
			if isinstance(kw['region'], basestring):
				cw = {'name': kw['region']}
				if country: cw['country'] = country
				region, created = Region.objects.get_or_create(**cw)
				kw['region'] = region
			else: region = kw['region']
		else: region = None

		if 'city' in kw:
			if isinstance(kw['city'], basestring):
				cw = {'name': kw['city']}
				if region: cw['region'] = region
				city, created = City.objects.get_or_create(**cw)
				kw['city'] = city

		return super(LocationManager, self).get_or_create(*av, **kw)
		
class Location(models.Model):
	__module__ = 'duetopia.register.models'
	address = models.CharField(max_length=256, blank=True, null=True, verbose_name=_('address'))
	city = models.ForeignKey(City, blank=True, null=True, verbose_name=_('city'))
	region = models.ForeignKey(Region, blank=True, null=True, verbose_name=_('region'))
	postcode = models.CharField(max_length=16, blank=True, null=True, verbose_name=_('postal code'))
	country = models.ForeignKey(Country, blank=True, null=True, verbose_name=_('country'))
	objects = LocationManager()
	class Meta:
		verbose_name = _('location')
		verbose_name_plural = _('locations')
	def __str__(self):
		parts = []
		if self.address: parts.append(self.address)
		if self.city and self.region:
			parts.append(str(self.city) + ', ' + str(self.region))
		elif self.city: parts.append(str(self.city))
		elif self.region: parts.append(str(self.region))
		if self.country: parts.append(str(self.country))
		if self.postcode: parts.append(str(self.postcode))
		return '\n'.join(parts)
	def save(self):
		if self.city:
			self.region = self.city.region
		if self.region:
			self.country = self.region.country
		super(Location, self).save()
